package 作业;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JComboBox;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.SQLException;

public class Maingui extends JFrame {

	private static final long serialVersionUID = 1L;
	private userinformation info;
	private JPanel contentPane;
	private tickets tick;
	private final ButtonGroup buttonGroup = new ButtonGroup();

	public Maingui(userinformation infos) throws SQLException {
		this.tick=new tickets();
		this.info=infos;
		setTitle("购票系统");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 807, 690);
		
		JMenuBar menuBar = new JMenuBar();
		menuBar.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
		setJMenuBar(menuBar);
		
		JMenu id = new JMenu("账户信息");
		menuBar.add(id);
		
		JMenuItem user = new JMenuItem("查看信息");
		user.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				(new userinfo(info)).setVisible(true);
			}
		});
		id.add(user);
		
		JMenuItem ticketinfor = new JMenuItem("修改密码");
		ticketinfor.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				(new editoruser(info)).setVisible(true);
			}
		});
		id.add(ticketinfor);
		
		JMenu ticket = new JMenu("购票记录");
		menuBar.add(ticket);
		
		JMenuItem history = new JMenuItem("查看记录");
		history.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//添加信息
				try {
					(new showticket("退票",info)).setVisible(true);
				} catch (SQLException ex) {
					throw new RuntimeException(ex);
				}
			}
		});
		ticket.add(history);
		
		JMenuItem getout = new JMenuItem("退票");
		getout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//退票信息
				try {
					(new showticket("退票",info)).setVisible(true);
				} catch (SQLException ex) {
					throw new RuntimeException(ex);
				}
			}
		});
		ticket.add(getout);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

		setContentPane(contentPane);
		
		JComboBox start = new JComboBox(tick.getcity());
		start.setFont(new Font("宋体", Font.PLAIN, 20));
		
		JComboBox end = new JComboBox(tick.getendcity());
		end.setFont(new Font("宋体", Font.PLAIN, 20));
		
		JLabel lblNewLabel = new JLabel("-->");
		lblNewLabel.setFont(new Font("宋体", Font.BOLD, 35));
		
		JLabel lblNewLabel_1 = new JLabel("选择起点和终点站");
		lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 30));
		
		JRadioButton aulit = new JRadioButton("成人票");
		aulit.setSelected(true);
		buttonGroup.add(aulit);
		aulit.setFont(new Font("宋体", Font.PLAIN, 20));
		
		JRadioButton child = new JRadioButton("学生票");
		buttonGroup.add(child);
		child.setFont(new Font("宋体", Font.PLAIN, 20));
		
		JLabel lblNewLabel_2 = new JLabel("购票类型：");
		lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 20));
		
		JButton get = new JButton("一键查询");
		get.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(((String)start.getSelectedItem()).equals((String)end.getSelectedItem())) {
					JOptionPane.showMessageDialog(null,"终点和目的地不能一样","提示",JOptionPane.ERROR_MESSAGE);
				}
				else {
					//车票展示页面
					try {
						(new showticket(info,tick.gett((String)start.getSelectedItem(),(String)end.getSelectedItem(),child.isSelected()))).setVisible(true);
					} catch (SQLException ex) {
						throw new RuntimeException(ex);
					}
				}
			}
		});
		get.setBackground(new Color(0, 255, 0));
		get.setFont(new Font("宋体", Font.PLAIN, 25));
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(156)
							.addComponent(start, GroupLayout.PREFERRED_SIZE, 177, GroupLayout.PREFERRED_SIZE)
							.addGap(48)
							.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(end, GroupLayout.PREFERRED_SIZE, 178, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(272)
							.addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 290, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(216)
							.addComponent(lblNewLabel_2, GroupLayout.PREFERRED_SIZE, 153, GroupLayout.PREFERRED_SIZE)
							.addGap(31)
							.addComponent(aulit, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(child, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(241)
							.addComponent(get, GroupLayout.PREFERRED_SIZE, 339, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(243, Short.MAX_VALUE))
		);
		
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(100)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addGap(37)
									.addComponent(start, GroupLayout.PREFERRED_SIZE, 52, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_contentPane.createSequentialGroup()
									.addGap(38)
									.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
										.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 53, GroupLayout.PREFERRED_SIZE)
										.addComponent(end, GroupLayout.PREFERRED_SIZE, 52, GroupLayout.PREFERRED_SIZE)))))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(33)
							.addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)))
					.addPreferredGap(ComponentPlacement.RELATED, 79, Short.MAX_VALUE)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_2, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
						.addComponent(aulit)
						.addComponent(child))
					.addGap(99)
					.addComponent(get, GroupLayout.PREFERRED_SIZE, 53, GroupLayout.PREFERRED_SIZE)
					.addGap(147))
		);
		contentPane.setLayout(gl_contentPane);
	}

}
